#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int MAXN = 50 + 10;
const int MOD = 1e9 + 7;
int t, n, m;
string s[MAXN];

int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    ios::sync_with_stdio(false), cin.tie(0);

    cin >> t;
    while (t--) {
        cin >> n >> m;
        for (int i = 1; i<= n; i++) cin >> s[i];
        int res = 1e9;
        for (int i = 1; i <= n; i++) {
            for (int j = i+1; j <= n; j++) {
                int cost = 0;
                for (int k = 0; k < m; k++) {
                    // cout << s[i][k] << ',' << s[j][k] << endl;

                    int d = s[i][k]-s[j][k];
                    // cout << d << endl;
                    cost += abs(d);
                }
                res = min(res, cost);
            }
        }
        cout << res <<endl;
    }
    return 0;
}